package com.example.examsystem2.controller;

import com.example.examsystem2.entity.PaperResult;
import com.example.examsystem2.entity.common.ResponseResult;
import com.example.examsystem2.service.ExamUserService;
import com.example.examsystem2.service.PaperResultService;
import com.example.examsystem2.util.JsonUtils;
import com.example.examsystem2.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("exam_system")
public class PaperResultController {

    @Autowired
    private PaperResultService paperResultService;

    @Autowired
    private ExamUserService examUserService;

    ResponseResult responseResult = new ResponseResult();


    @RequestMapping("/add_user_paper_result")
    @ResponseBody
    public String add_user_paper_result(String cq_res_data, String jq_res_data, String sa_res_data, String fbq_res_data, int eu_id) {
        /*  String sa_res = "";
        if (sa_res_data.length > 1) {
            sa_res = ArrayUtils.toString(sa_res_data, "&&&");
            System.out.println(sa_res);
        }*/
        PaperResult paperResult =new PaperResult();
        paperResult.setEu_id(eu_id);
        paperResult.setPr_cq_res(cq_res_data);
        paperResult.setPr_jq_res(jq_res_data);
        paperResult.setPr_saq_res(sa_res_data);
        paperResult.setPr_fbq_res(fbq_res_data);
        paperResult.setPr_creat_time(TimeUtils.getTime());
        paperResult.setPr_state("未审阅");
        paperResult.setPr_cq_score(0);
        paperResult.setPr_jq_score(0);
        paperResult.setPr_saq_score(0);
        paperResult.setPr_fbq_score(0);
        paperResult.setPr_total_score(0);
        int i = paperResultService.add_user_paper_result(paperResult);
        if (i==1){
            int i1 = examUserService.update_exam_user_by_eu_id(eu_id);
            if (i1==1){
                responseResult.setCode(200);
                responseResult.setMessage("已交卷!等待审阅");
            }
            else {
                responseResult.setCode(0);
                responseResult.setMessage("提交失败!");
            }
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("提交失败!");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/update_user_paper_result_by_eu_id")
    @ResponseBody
    public String update_user_paper_result_by_eu_id(PaperResult paperResult){
        paperResult.setPr_state("已审阅");
        int i = paperResultService.update_user_paper_result_by_eu_id(paperResult);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("审阅成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("审阅失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/get_no_review_exam")
    @ResponseBody
    public String get_no_review_exam(){
        List<PaperResult> no_review_exam = paperResultService.get_no_review_exam();
        responseResult.setCode(0);
        responseResult.setMessage("成功");
        responseResult.setResult(no_review_exam);
        return JsonUtils.getJson(responseResult);
    }





}
